package com.naiterui.ehp.bs.im.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.security.HeaderConstant;
import com.naiterui.ehp.bp.support.security.ImLoginInterceptor;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.context.SpringContextHolder;
import com.naiterui.ehp.bs.im.exception.ExceptionCodes;
import com.naiterui.ehp.bs.im.remote.IImWeixinRemoter;
import com.naiterui.ehp.bs.im.remote.IPcRemoter;
import com.naiterui.ehp.bs.im.remote.IRemoter;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户登录校验器
 */
@Slf4j
public class LoginInterceptor extends ImLoginInterceptor {

    @Override
    protected boolean verifyToken(HttpServletRequest request, HttpServletResponse response) throws Exception {

        boolean result = false;

        String origin = request.getHeader(HeaderConstant.HEADER_ORIGIN);

        String doctorId = request.getParameter(SettingConfig.DOCTOR_ID);
        String patientId = request.getParameter(SettingConfig.PATIENT_ID);
        String token = request.getParameter(SettingConfig.TOKEN);

        Long id = null;
        if (StringUtils.isNumeric(origin)) {
            switch (Integer.parseInt(origin)) {
                case HeaderConstant.HEADER_ORIGIN_PATIENT_APP:
                    // 如果是医生pc端和小程序端的token 是通过header头传递的
                    token = request.getHeader(SettingConfig.TOKEN_HEADER);
                    // String token = request.getHeader(tokenHeader);
                    id = this.checkMiniToken(token);
                    if (id != null) {
                        log.info("资源持有器线程上下文-存入数据-IM，patientId {}", id);
                        ResourceHolder.bindResource(SettingConfig.PATIENT_ID, id);
                        result = true;
                    }
                    // request.setAttribute(SettingConfig.PATIENT_ID, id);
                    break;

                case HeaderConstant.HEADER_ORIGIN_PATIENT_WX:
                    result = this.checkPtToken(patientId, token);
                    break;

                case HeaderConstant.HEADER_ORIGIN_DOCTOR_APP:
                    result = this.checkDrToken(doctorId, token);
                    break;
                case HeaderConstant.HEADER_ORIGIN_DOCTOR_PC:
                    id = this.checkDoctorPcToken(token);
                    if (id != null) {
                        ResourceHolder.bindResource(SettingConfig.DOCTOR_ID, id);
                        result = true;
                    }
                    break;
                default:
                    result = false;
                    break;
            }
        }

        if (!result) {
            response.getWriter().println(JsonMapper.toJson(PublicService.returnResponseVO(ExceptionCodes.TOLOGIN)));
        }

        return result;
    }

    private boolean checkPtToken(String patientId, String token) throws BusinessException {

        boolean result = false;
        if (!StringUtils.isNumeric(patientId) || StringUtils.isBlank(token)) {
            return result;
        }
        IRemoter remoter = SpringContextHolder.getBean("remoter", IRemoter.class);
        result = remoter.checkPtToken(patientId, token);

        return result;
    }

    private boolean checkDrToken(String doctorId, String token) throws BusinessException {

        boolean result = false;
        if (!StringUtils.isNumeric(doctorId) || StringUtils.isBlank(token)) {
            return result;
        }
        IRemoter remoter = SpringContextHolder.getBean("remoter", IRemoter.class);
        result = remoter.checkDrToken(doctorId, token);
        return result;
    }

    private Long checkMiniToken(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        IImWeixinRemoter remoter = SpringContextHolder.getBean(IImWeixinRemoter.class);
        try {
            Long id = remoter.checkMiniToken(token);
            return id;
        } catch (BusinessException e) {
            log.error("校验小程序token失败", e);
        }
        return null;
    }

    private Long checkDoctorPcToken(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        IPcRemoter remoter = SpringContextHolder.getBean(IPcRemoter.class);
        try {
            Long id = remoter.checkDoctorPcToken(token);
            log.info("checkDoctorPcToken id:{}", id);
            return id;
        } catch (BusinessException e) {
            log.error("校验医生pc端token失败", e);
        }
        return null;
    }
}
